package com.lanou.lenanapp.adapter;

import android.content.Context;
import android.graphics.Movie;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.db.DatabaseHelper;
import com.lanou.lenanapp.entity.greendao.CollectImageEntity;
import com.lanou.lenanapp.view.GifMovieView;
import com.lanou.lenanapp.volley.StreamRequest;
import com.lanou.lenanapp.volley.VolleySingleton;


import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 收藏里的图片收藏适配器
 */
public class CollectImageAdapter extends RecyclerView.Adapter<CollectImageAdapter.CollectImageViewHolder> {

    private List<CollectImageEntity> list;
    private Context context;
    private ImageLoader imageLoader;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private DatabaseHelper helper;

    public void addData(List<CollectImageEntity> datas){
        this.list = datas;
    }

    public CollectImageAdapter(Context context) {
        super();
        this.context = context;
        imageLoader = volleySingleton.getImageLoader();
    }

    class CollectImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView updata_time_image, wbody_image, comments_image, like_image;
        private ImageView like_one_image, like_image_image, favorited;
        private NetworkImageView imageIv;
        private GifMovieView gifMovieView;
        private int position;

        public CollectImageViewHolder(View itemView) {
            super(itemView);
            helper = DatabaseHelper.getHelper();
            updata_time_image = (TextView) itemView.findViewById(R.id.updata_time_image);
            wbody_image = (TextView) itemView.findViewById(R.id.wbody_image);
            comments_image = (TextView) itemView.findViewById(R.id.comments_image);
            imageIv = (NetworkImageView) itemView.findViewById(R.id.imageIv);
            gifMovieView = (GifMovieView) itemView.findViewById(R.id.gifMovieView);
            like_one_image = (ImageView) itemView.findViewById(R.id.like_one_image);
            like_image_image = (ImageView) itemView.findViewById(R.id.like_image_image);
            like_image = (TextView) itemView.findViewById(R.id.like_image);
            favorited = (ImageView) itemView.findViewById(R.id.favorited);
            favorited.setOnClickListener(this);
        }

        //收藏的点击事件
        @Override
        public void onClick(View v) {
            int position = getLayoutPosition();
            favorited.setImageResource(R.drawable.icon_favorite);
            CollectImageEntity entities = list.get(position);//获得点击的行布局位置
            list.remove(position);
            long id = entities.getId();//获得主键
            helper.deleteByIdCollectImage(id);//按主键删除
            Toast.makeText(context, "已经取消收藏....", Toast.LENGTH_SHORT).show();
            notifyItemRemoved(position);
            if (listener != null){
                listener.OnRemoveImageClick(list);
            }
        }
    }

    @Override
    public CollectImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.fragment_collect_image_gut, null);
        return new CollectImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CollectImageViewHolder holder, int position) {
        if (list != null && list.size() > 0){
            CollectImageEntity entity = list.get(position);
            holder.position = position;

            //时间转换
            String time = getNormalTime(entity.getUpdate_time());
            holder.updata_time_image.setText(time);

            holder.wbody_image.setText(entity.getWbody());
            //设置赞的数量(返回的赞的数据是小数的字符串)
            //先转换成double格式
            double d = Double.parseDouble(entity.getLikes());
            int t = (int) d;//再强制转换成int类型(因为string不能直接转换成int类型)
            holder.like_image.setText(t + "");//在转换成字符串格式显示

            holder.comments_image.setText(entity.getComments());

            int is = Integer.parseInt(entity.getIs_gif());
            String url = entity.getWpic_middle();
            if (is == 0) {

                holder.gifMovieView.setVisibility(View.GONE);
                holder.imageIv.setVisibility(View.VISIBLE);
                //图片
                getBitMap(url, holder.imageIv);

            }else if(is == 1) {
                holder.imageIv.setVisibility(View.GONE);
                holder.gifMovieView.setVisibility(View.VISIBLE);

                getGifMove(url, holder.gifMovieView);
            }

        }

    }

    /**
     * 时间格式转换方法
     * **/
    public  String  getNormalTime( String value) {
        Long l = Long.valueOf(value);
        l = l * 1000;
        Date date = new Date(l);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String time = format.format(date) ;
        return time;
    }

        /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }

    /**
     * 动态图片的拉取
     * **/
    private void getGifMove(String url, final GifMovieView gifMovieView) {
        StreamRequest request = new StreamRequest(url, new Response.Listener<InputStream>() {
            @Override
            public void onResponse(InputStream response) {
                Movie movie = Movie.decodeStream(response);
                gifMovieView.setMovie(movie);
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);

    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }


    //定义接口 回调...
    public interface OnRemoveImageClickListener{
        void OnRemoveImageClick(List<CollectImageEntity> list);
    }
    private OnRemoveImageClickListener listener;
    public void setOnRemoveImageClickListener(OnRemoveImageClickListener listener){
        this.listener = listener;
    }


}
